<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mares extends CI_Controller {

	/**
	 * Se establece el constructor para realizar la carga de librerías y modelos pertinentes a la clase.
	 * */
	function __construct()
	{   // Call the Model constructor
		parent::__construct();
		$this->load->database('mares');
		//$this->load->model('secure_model');
	}

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('welcome_message');

	} // End-index-function


	private function val_usr_micro($id){

		// Consultamos la base de datos de mares para encontrar el usuario con $id
		$db = $this->load->database('mares', TRUE);
		$query = $db->query('SELECT paquete_consultas_varias.info_alumno('.$id.') FROM DUAL');
		echo "<table border='1'>\n";
		foreach ($query->result() as $row){
			echo "<tr>\n";
			foreach ($row as $item) {
				echo "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
			}
			echo "</tr>\n";
		}
		echo "</table>\n";

		echo 'Total Results: ' . $query->num_rows();
	}

	private function ver($id,$key){
		// Desencriptamos la información pasada
		$this->load->model('secure_model');
		$id = $this->secure_model->desencript($id);
		$key = $this->secure_model->desencript($key);
		// realizamos la consulta con el portal Universitario.
		$res = $this->secure_model->vermares($id, $key);
		// Encriptamos y enviamos la respuesta.
		return $this->secure_model->encript($res);
		//$this->val_usr_micro($id);
		//$this->val_usr_micro('1036621041');
	}

	function verifica_usr($id){
		
		$this->load->model("mares_model");
		$usr = $this->mares_model->info_alumno($id);
		foreach ($usr as $v) {
			echo $v."<br>";
		}
		return;
	} // End-function-verificar_usr

	function ver_usr($id,$key){

		$this->load->model('secure_model');
		//echo $this->secure_model->desencript($id);
		//echo $this->secure_model->desencript($key);
		// Validar si los datos son aceptables
		//$id = str_replace("-", "/", $id);
		//$pwd = str_replace("-", "/", $pwd);
		//$id = $this->secure_model->desencript($id);
		//$key = $this->secure_model->desencript($key);
		//echo $this->verifica_usr($id,$key);
		
		$res = $this->secure_model->vermares($id,$key);
		echo $res;
		//echo $this->secure_model->encript($res);
	}

	function est_pgm_activo($id){
		//$dbl = $this->load->database('default', TRUE);
		// Retorna programa que está activo el estudiante
		$this->load->model("mares_model");
		$usr = $this->mares_model->est_pgm_activo($id);
		if (isset($usr)) {
			foreach ($usr as $v) {
				echo $v."<br>";
			}
			return;
		}
		
		
		$sql = "SELECT paquete_consultas_varias.estud_programas(?) FROM dual";
		$query = $this->db->query($sql, $id);
		
		foreach ($query->result() as $row){
			foreach ($row as $item) {
				// Debo verificar que el usuario pertenezca a microbiología
				$datos = ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;");
				$dt = explode("!",$datos);
// 				$o = new ArrayObject();
				foreach ($dt as $d) {
					$v= explode("|",$d);
					if (in_array($v[0],array(90504,504,785,784,783,50058,60110,60111))
							 && in_array($v[1],array('ACTIVO','GRADUADO'))) {
						$o[$v[1]] = $v[0];
					}
				}
				ksort($o);
				return $o;
// 				foreach ($o as $key => $val) echo "$key = $val<br/>";
			}
		} // End-For

	} // EndFunction-est_pgm_activo

} //End-Class-Mares


/* End of file mares.php */
/* Location: ./application/controllers/mares.php */